/*********** NOTES HEADER *********** 

DESCRIPTION: This program estimates the matched fixed effects logit
	and the marginal effects

Datasets used:
 (1) MatchedSample_Full

Datasets created:
 (1) diab_fe 
 (2) hyp_fe
 (3) death_fe 
 
************************************/ 

set more off
capture log close
clear all

global origData "\\192.168.6.107\silstorage\MedicareClaims-P045601-BE"
global dataIn "\\192.168.6.107\silstorage\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-In\"
global dataOut "\\192.168.6.107\silstorage\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\"
global dpath "\\192.168.6.107\silstorage\MedicareClaims-P045601-BE\Work\ay_data"
global dataProp "\\192.168.6.107\silstorage\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\PropScore\Patients"
global logs "\\192.168.6.107\silstorage\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Logs\PropScore\Patients"
global dpath "\\192.168.6.107\silstorage\MedicareClaims-P045601-BE\Work\ay_data"
global skapath "\\192.168.6.107\silstorage\MedicareClaims-P045601-BE\Work\ska"

adopath +  "\\192.168.6.107\silstorage\SIL-Common/estout"
adopath +  "\\192.168.6.107\silstorage\SIL-Common/outreg2"
adopath +  "\\192.168.6.107\silstorage\SIL-Common/reghdfe-master/package"

cd "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Program\PropensityScore\Patients\ResponsetoReferee\OtherPropScoreMethods\BiasAdjustment"

log using "./3.Overall_fe.log", replace

	program getColnames 
	 quietly {
	  matrix A = e(b)'
	 
	 local rownames : rowfullnames A
	 local c : word count `rownames'
	 local names : colfullnames A

	 svmat A, names(col)

	 gen rownames = ""
	 forvalues i = 1/`c' {

	  replace rownames = "`:word `i' of `rownames''" in `i' 
	   } 

	 }

	end


	program getRegStats
	 quietly {
	  matrix A = e(b)'
	 
	 local rownames : rowfullnames A
	 local c : word count `rownames'
	 local n = `c' + 1
	 local pll = `c' + 2
	 local rsq = `c' + 3
	 local ndrop = `c' + 4

	 replace y1 =e(N) in `n' 
	 replace y1 =e(ll) in `pll' 
	 replace y1 =e(r2_p) in `rsq' 
	 replace y1 =e(N_drop) in `ndrop' 

	 }

	end

use  "./MatchedSample_Full.dta"
gen season = quarter(quarter)

 
** START STATISTICS **

	*Diabetes
	foreach var of varlist oc_diabcomp1 oc_glaucoma oc_diabcomp2 {
	*This limits the sample to diabetes patients
	 forval j=3(1)3  {

	if regexm("`var'","oc_([a-z0-9]+)$") { 
	  local pre = "`=regexs(1)'"

	capture rename mxicd3_chronic mxicd3_endocrine 
	clogit `var'  i.postmerger#i.sex_pat i.vmerger##i.sex_pat i.vmerger#c.nyear i.p_age i.agecat##i.sex_pat nonwhite#i.agecat i.season i.year mxicd1_acute mxicd5_acute mxicd18_acute mxicd*_c* fmp pspcl30 pspcl06 pspcl69 pspcl18 pspcl48 pspcl93 pspclOther fs* lnvisit* if flag`j'==1 & kv_`pre'==1 , group(propScoreId) cluster(propScoreId)   

	  matrix sesq = vecdiag(e(V))' 
	  svmat sesq
	  getColnames
	  getRegStats

	  rename rownames rn`pre'_`j'
	  capture drop j 
	  capture drop i
	  rename y1 beta_`pre'_`j'
	  rename sesq1 sesq_`pre'_`j'

	   }
	  }
	 }

	 save "./diab_fe.dta", replace

	rename mxicd3_endocrine mxicd3_chronic 
	capture rename mxicd8_circulatory mxicdCIRCULATORY

		*Hypertension
		foreach var of varlist oc_ami oc_acuteicd8 oc_ihd  {
		*This limits the sample to hyptertensive patients
		 forval j=2(1)2  {

		if regexm("`var'","oc_([a-z0-9]+)$") { 
		  local pre = "`=regexs(1)'"

		clogit `var'  i.postmerger#i.sex_pat i.vmerger##i.sex_pat i.vmerger#c.nyear i.p_age nonwhite#i.agecat i.agecat##i.sex_pat i.season i.year mxicd1_acute mxicd5_acute mxicd18_acute mxicd*_c* fmp pspcl30 pspcl06 pspcl69 pspcl18 pspcl48 pspcl93 pspclOther fs* lnvisit* if flag`j'==1 & kv_`pre'==1 , group(propScoreId) cluster(propScoreId) 
		  matrix sesq = vecdiag(e(V))' 
		  svmat sesq
		  getColnames
		  getRegStats

		  rename rownames rn`pre'_`j'
		  capture drop j 
		  capture drop i
		  rename y1 beta_`pre'_`j'
		  rename sesq1 sesq_`pre'_`j'

			 }
		  }
		}

		save "./hyp_fe.dta", replace

		replace state=0 if state==7 | state==39
		capture rename mxicdCIRCULATORY mxicd8_circulatory 

		foreach var of varlist oc_death  {
		 forval j=1(1)3  {

		if regexm("`var'","oc_([a-z0-9]+)$") { 
		  local pre = "`=regexs(1)'"

		clogit `var'  i.postmerger#i.sex_pat i.vmerger##i.sex_pat i.vmerger#c.nyear i.p_age i.agecat##i.sex_pat nonwhite#i.agecat i.season i.year  mxicd1_acute mxicd5_acute mxicd18_acute mxicd*_c* fmp pspcl30 pspcl06 pspcl69 pspcl18 pspcl48 pspcl93 pspclOther fs* lnvisit* if flag`j'==1 & kv_`pre'==1 , group(propScoreId) cluster(propScoreId)
		   matrix sesq = vecdiag(e(V))' 
			svmat sesq
		  getColnames
		  getRegStats

		  rename rownames rn`pre'_`j'
		  capture drop j 
		  capture drop i
		  rename y1 beta_`pre'_`j'
		  rename sesq1 sesq_`pre'_`j'

		   }
		  }
		}

		save "./death_fe.dta", replace
		clear
		 